/*
 * @Author: doing
 * @Date: 2023-08-09 21:10:33
 * @LastEditors: git config user.name && git config user.email
 * @LastEditTime: 2023-08-20 07:48:11
 * @FilePath: \线狮少年\src\store\modules\users.js
 * @Description: 
 * 用户管理模块的公共状态
 * Copyright (c) 2023 by 1540265624@qq.com, All Rights Reserved. 
 */
import { 
    updataUser,
    searchUser,
    getUserTotal,
    searchRole,
    getUserRoleId
 } from '~/api/user.js'
const state = () => ({
    name: '我是user模块',
    userList: [],
    userTotal: 0,
    roleList: [],
    roleids: [],
    roleTotal: 0
})
const getters = {
    getUserList: (state, getters, rootState) => {
        
    }
}
const mutations = {
    setUserList (state,params){
        state.userList = params
    },
    setUserTotal(state,params){
        state.userTotal = params.data.data
    },
    setRoleList(state,params){
        state.roleList = params
    },
    setRoleIds(state,params){
        state.roleids = params
    }
}

const actions = {
    async getUserList({ commit,state }, params){
        //变量
        try{
            await searchUser(params).then(res=>{
                commit('setUserList',res.data.data.records)
            })
            await getUserTotal().then(res=>{
                commit('setUserTotal',res)
            })
        } catch (e){
            console.log(e)
        }
    },
    async getRoleList({commit,state},params){
        try{
            await searchRole().then(res=>{
                commit('setRoleList',res)
            })
        } catch(e){
            console.log(e)
        }
    },
    async getRoleids({commit,state},params){
        try{
            await getUserRoleId().then(res=>{
                commit('setRoleIds',res)
            })
            await state.roleids.forEach(async (item1)=>{
                await state.roleList.forEach(item2=>{
                    if(item1 === item2.id){
                        console.log('true')
                    }
                })
            })
        } catch(e){
            console.log(e)
        }
    },
    async text({commit,state},params){
        try{
            await updataUser().then(res=>{
                console.log(res)
            })
        } catch(e){
            console.log(e)
        }
    }
}
export default {
    namespaced: true,
    state,
    getters,
    actions,
    mutations,
    //嵌套模块
    // modules:{
    //     nested
    // }
}
